package com.ztools.pc.provider;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.ztools.api.service.pc.PcCouponService;
import com.ztools.common.model.pc.coupon.CouponExpire;
import com.ztools.common.model.pc.coupon.CouponRecord;
import com.ztools.common.model.pc.coupon.CouponUsed;
import com.ztools.pc.service.coupon.CouponExpireService;
import com.ztools.pc.service.coupon.CouponRecordService;
import com.ztools.pc.service.coupon.CouponUsedService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static com.ztools.common.util.pc.RedisUtil.CACHE_COUPON_PACKET;

/**
 * @Author: seeTools
 * @Date: 2023/5/14  12:04
 * @Version 1.0
 */
@Service(version = "1.0", timeout = 3000)
public class PcCouponServiceImpl implements PcCouponService {

    @Resource
    private CouponRecordService couponRecordService;
    @Resource
    private CouponUsedService couponUsableService;
    @Resource
    private CouponExpireService couponExpireService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * @param memberId memberId
     * @return CouponRecord
     */
    @Override
    public List<CouponRecord> getCouponInfo(Long memberId) {
        CouponRecord record = CouponRecord.builder()
                .memberId(memberId)
                .state(0)
                .build();
        return couponRecordService.findByObject(record);
    }

    @Override
    public List<CouponRecord> getCouponTypeNumber(Long memberId) {
        return couponRecordService.getCouponTypeNumber(memberId);
    }

    @Override
    public List<CouponRecord> findByObject(CouponRecord couponRecord) {
        return couponRecordService.findByObject(couponRecord);
    }

    @Override
    public CouponRecord getCouponPacket(CouponRecord couponRecord) {
        // 会员券包缓存
        String key = CACHE_COUPON_PACKET + couponRecord.getMemberId() + ":" + couponRecord.getCouponType();
        String redis = stringRedisTemplate.opsForValue().get(key);
        if (JSONUtil.isJson(redis) && StrUtil.isNotEmpty(redis)) {
            return JSONUtil.toBean(redis, CouponRecord.class);
        }
        // 未使用的优惠券
        List<CouponRecord> couponRecordList = couponRecordService.findByObject(couponRecord);
        couponRecordService.dictionary(couponRecordList);
        couponRecordList = couponRecordList.stream().sorted(Comparator.comparing(CouponRecord::getTemplateName)).collect(Collectors.toList());
        couponRecord.setUsableList(couponRecordList);

        // 已使用的优惠券
        CouponUsed couponUsed = CouponUsed.builder()
                .couponType(couponRecord.getCouponType())
                .memberId(couponRecord.getMemberId())
                .build();
        List<CouponUsed> couponUsedList = couponUsableService.findByObject(couponUsed);
        couponUsableService.dictionary(couponUsedList);
        couponUsedList = couponUsedList.stream().sorted(Comparator.comparing(CouponUsed::getTemplateName)).collect(Collectors.toList());
        couponRecord.setUsedList(couponUsedList);

        // 已过期的优惠券
        CouponExpire couponExpire = CouponExpire.builder()
                .couponType(couponRecord.getCouponType())
                .memberId(couponRecord.getMemberId())
                .build();
        List<CouponExpire> couponExpireList = couponExpireService.findByObject(couponExpire);
        couponExpireService.dictionary(couponExpireList);
        couponRecord.setExpireList(couponExpireList);
        stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(couponRecord), 10, TimeUnit.MINUTES);
        return couponRecord;
    }

    @Override
    public CouponRecord getCouponUsable(CouponRecord couponRecord) {
        // 会员券包缓存
        String key = CACHE_COUPON_PACKET + couponRecord.getMemberId() + ":" + "usable";
        String redis = stringRedisTemplate.opsForValue().get(key);
        if (JSONUtil.isJson(redis) && StrUtil.isNotEmpty(redis)) {
            return JSONUtil.toBean(redis, CouponRecord.class);
        }
        CouponRecord record = new CouponRecord();
        // 未使用的优惠券
        List<CouponRecord> couponRecordList = couponRecordService.getCouponUsable(couponRecord);
        record.setUsableList(couponRecordList);
        stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(record), 10, TimeUnit.MINUTES);
        return record;
    }

}
